// pages/index/activity/style/style.js
import Toast from "../../../../miniprogram_npm/@vant/weapp/toast/toast"

const app = getApp()
//获取 log 日志打印
const log = new app.appLogs()
//获取 http 网络请求
const http = new app.appHttp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    theme: {},
    //标题tab
    tabs: [{
      id: "最新",
      title: "最新风采"
    },{
      id: "人气",
      title: "人气风采"
    },{
      id: "游客",
      title: "游客风采"
    }],
    //活动id
    activityId: null,
    //院落id  默认最新 获取最新数据
    yardId: '最新',
    pageNumber: 1,
    pageSize: 1000,
    //风采列表
    list: [],
    isLoading: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '活动风采'
    })
    this.setData({
      theme: app.globalData.theme,
      activityId: options.id
    })
    // this.getYardList()
    this.getStyleList()
  },

  /**
   * 获取院落列表 （小区列表）
   */
  getYardList: function () {
    http.get('yard/select/area_code',{
      area_code: app.globalData.areaCode,
    }).then(res => {
      if (res.data.code === http.REQUEST_OK) {
        //添加最新和人气项
        let list = [{
          id: "最新",
          title: "最新风采"
        },{
          id: "人气",
          title: "人气风采"
        },{
          id: "游客",
          title: "游客风采"
        }]
        res.data.data.forEach(item => {
          list.push({
            id: item.yard_id,
            title: item.yard_name
          })
        })
        this.setData({
          tabs: list
        })
      }else {
        Toast.fail("获取院落列表失败")
      }
    }, err => {
      Toast.fail("网络异常")
    })
  },

  /**
   * 获取风采列表
   */
  getStyleList: function () {
    this.setData({
      isLoading: true
    })
    http.get("activity-style/list",{
      page_number: this.data.pageNumber,
      page_size: this.data.pageSize,
      activity_id: this.data.activityId,
      yard_id: this.data.yardId
    }).then(res => {
      if (res.data.code === http.REQUEST_OK) {
        let list = []
        res.data.data.list.forEach(item => {
          item.create_time = item.create_time.substr(0,10)
          list.push(item)
        })
        this.setData({
          list: list
        })
      }else {
        Toast.fail(res.data.msg)
      }
      this.setData({
        isLoading: false
      })
    }, err => {
      Toast.fail("网络异常")
      this.setData({
        isLoading: false
      })
    })
  },

  /**
   * 当前激活的标签改变时触发
   * @param e
   */
  onTabChange(e) {
    log.e(JSON.stringify(e))
    const index = e.detail.index
    log.e(index)
    this.setData({
      list: [],
      pageNumber: 1,
      pageSize: 10,
    })
    this.data.yardId = this.data.tabs[index].id
    this.getStyleList()
  },

  /**
   * 点赞 (取消点赞)
   * @param e
   */
  onLike: function (e) {
    const index = e.currentTarget.dataset.index
    const id = e.currentTarget.dataset.id
    log.e(index)
    http.post('activity-like/like',{
      id: id,
      type: 2
    }).then(res => {
      if (res.data.code === http.REQUEST_OK) {
        this.getStyleList()
      }else {
        Toast.fail("失败")
      }
    }, err => {
      Toast.fail("网络异常")
    })
  },

  onStyleDetail: function (e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: "./style_detail/style_detail?id=" + id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})